﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestaurateurConsole;

namespace RestaurateurConsole.Core
{
    public class CustomerHandler
    {
        DataEncapsulator Data;
        private CustomerHandler() { }
        public CustomerHandler(DataEncapsulator Data)
        {
            this.Data = Data;
            UpdateCustomers();
        }

        private IEnumerable<Customer> customers;
        public IEnumerable<Customer> Customers
        {
            get
            {
                return customers;
            }
        }

        public void Save()
        {
            Data.DB.SubmitChanges();
        }

        private void UpdateCustomers()
        {
            customers = from c in Data.DB.Customers select c;
        }

        public int Add(string name)
        {
            int CustomerID;
            if (customers.Count() == 0)
                CustomerID = 1;
            else
                CustomerID = customers.Max(C => C.CustomerID) + 1;

            Customer newCust = new Customer();
            newCust.CustomerID = CustomerID;
            newCust.CustomerName = name;

            Data.DB.Customers.InsertOnSubmit(newCust);

            Data.DB.SubmitChanges();
            UpdateCustomers();

            return CustomerID;
        }

        public int Add(string name, string phone)
        {
            int CustomerID;
            if (customers.Count() == 0)
                CustomerID = 1;
            else
                CustomerID = customers.Max(C => C.CustomerID) + 1;

            Customer newCust = new Customer();
            newCust.CustomerID = CustomerID;
            newCust.CustomerName = name;
            newCust.CustomerPhone = phone;

            Data.DB.Customers.InsertOnSubmit(newCust);

            Data.DB.SubmitChanges();
            UpdateCustomers();

            return CustomerID;
        }

        public IEnumerable<Customer> Filter(string name)
        {
            return from d in Data.DB.Customers
                   where d.CustomerName.IndexOf(name) != -1
                   select d;
        }


        public void Remove(int custID)
        {
            var c = customers.First(C => C.CustomerID == custID);
            if (c != null)
            {
                Data.DB.Customers.DeleteOnSubmit(c);
                Data.DB.SubmitChanges();
            }
            UpdateCustomers();
        }

        public void Update(int custID, string phone)
        {
            var c = customers.First(C => C.CustomerID == custID);
            if (c != null)
            {
                c.CustomerPhone = phone;
                Data.DB.SubmitChanges();
            }
            UpdateCustomers();
        }

        public IEnumerable<CustomerBalance> GetBalance(IEnumerable<Customer> Customers, ActivityHandler activityHandler, SalesHandler salesHandler)
        {
            foreach (var c in Customers)
                yield return GetBalance(c, activityHandler, salesHandler);
        }

        public CustomerBalance GetBalance(Customer c, ActivityHandler activityHandler, SalesHandler salesHandler)
        {
            var activities = from act in activityHandler.Activities
                             where act.CustID == c.CustomerID
                             select act;

            decimal purchases = 0;
            decimal payments = 0;

            foreach (var a in activities)
            {
                //If the activity has any payments made, note it.
                if (a.Payment.HasValue)
                    payments += a.Payment.Value;

                //If the activity has any puchases associated, get the billing amount and note it.
                if (a.SalesID.HasValue)
                {
                    var sales = from sale in salesHandler.Sales
                                where sale.SalesID == a.SalesID
                                select sale;
                    foreach (var s in sales)
                    {
                        purchases += s.Amount;
                    }
                }
            }

            return new CustomerBalance()
            {
                CustID = c.CustomerID,
                CustName = c.CustomerName,
                CustPhone = c.CustomerPhone,
                Purchases = purchases,
                Payments = payments,
                Balance = purchases - payments
            };
        }

    }
}
